package com.zhc.dejingapi.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhc.dejingapi.entity.QingZbEqLoadTableEntity;
import com.zhc.dejingapi.entity.vo.EqTypeNumVo;
import com.zhc.dejingapi.entity.vo.LoadVo;
import com.zhc.dejingapi.mapper.*;
import com.zhc.dejingapi.service.QingZbEqLoadTableService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhc
 * @since 2024-09-13
 */
@Service
public class QingZbEqLoadTableServiceImpl extends ServiceImpl<QingZbEqLoadTableMapper, QingZbEqLoadTableEntity> implements QingZbEqLoadTableService {

    @Resource
    private QingZbEqLoadTableMapper qingZbEqLoadTableMapper;

    @Resource
    private QingZbProcessrouteMapper qingZbProcessrouteMapper;
    @Resource
    private QingZbProcesswithrouteMapper qingZbProcesswithrouteMapper;
    @Resource
    private QingZbProductionOrderPoolMapper qingZbProductionOrderPoolMapper;
    @Resource
    private QingZbProductionOrderPoolTranMapper qingZbProductionOrderPoolTranMapper;
    @Resource
    private QingZbProductProcessrouteMapper qingZbProductProcessrouteMapper;
    @Resource
    private QingZbProcessPlanMapper qingZbProcessPlanMapper;

    @Resource
    private QingZbEqMapper qingZbEqMapper;
    @Override
    public List<QingZbEqLoadTableEntity> computeLoadWithEq(String workDate) {
        Date date = DateUtil.date();
        String format = DateUtil.format(date, "yyyy-MM");

        //if(Objects.equals(workDate, format)){
            //查询工序计划设备类型
            List<String> stringList = qingZbProcessPlanMapper.selectEq(workDate);
            //查询订单池设备类型
            List<String> stringList1 = qingZbProcessPlanMapper.selectEq1(workDate);

            List<EqTypeNumVo> eqTypeNumVos = qingZbEqMapper.selectEqType();
            //汇总设备类型
            List<String> mergedList = new ArrayList<>(new HashSet<>(stringList));
            for(String s:stringList1){
                if(!mergedList.contains(s)){
                    mergedList.add(s);
                }
            }
            //查询工序计划
            List<LoadVo> plans = qingZbProcessPlanMapper.selectPlan(workDate);
            //查询订单池计划
            List<LoadVo> prods = qingZbProcessPlanMapper.selectProd(workDate);
//        for (LoadVo p:prods){
//            for (EqTypeNumVo e:eqTypeNumVos){
//                if(p.getEqTypeName().equals(e.getEqTypeName())){
//                    p.setUntranQuantity(p.getUntranQuantity()/e.getNum());
//                }
//            }
//
//        }
            String now = workDate+"-01";
            Date date1 = DateUtil.parse(now);
            DateTime monthBegin = DateUtil.beginOfMonth(date1);
            DateTime monthEnd = DateUtil.endOfMonth(date1);
            for (String s : mergedList){
                if(s!=null){

                    int eqUseTimeQ=0;
                    //设备使用时间
                    int eqUseTime=0;
                    float workNeedTime= 0.0F;
                    String eqTypeName = null;
//            Date PlanStartTime = null;
//            Date PlanEndTime = null;
                    //计算工序计划中各设备类型使用时间
                    for(LoadVo p:plans){
                        if(p.getEqTypeCode()!=null){
                            if(p.getEqTypeCode().equals(s)){
                                eqTypeName=p.getEqTypeName();
                                Date planStartTime = p.getPlanStartTime();
                                Date planEndTime = p.getPlanEndTime();
                                //PlanStartTime=planStartTime;
                                //PlanEndTime=planEndTime;
                                boolean inStart = DateUtil.isIn(planStartTime, monthBegin, monthEnd);
                                boolean inEnd = DateUtil.isIn(planEndTime, monthBegin, monthEnd);
                                long dayPlan = DateUtil.between(planStartTime, planEndTime, DateUnit.DAY);
                                if(inStart){
                                    if(inEnd){

                                        eqUseTime = (int) dayPlan * 12*60*60;
                                        if(eqUseTimeQ<eqUseTime){
                                            eqUseTimeQ=eqUseTime;
                                        }
                                        workNeedTime+=(float) p.getCurrentProductionNum()*p.getStandardWorkingHour();
                                    }else {
                                        long days = DateUtil.between(planStartTime, monthEnd, DateUnit.DAY);
                                        eqUseTime = (int) days * 12*60*60;
                                        if(eqUseTimeQ<eqUseTime){
                                            eqUseTimeQ=eqUseTime;
                                        }
                                        workNeedTime+=(((float) p.getCurrentProductionNum()/dayPlan*days)*p.getStandardWorkingHour());
                                    }

                                }else {
                                    if(inEnd){
                                        long days = DateUtil.between(monthBegin, planEndTime, DateUnit.DAY);
                                        eqUseTime = (int) days * 12*60*60;
                                        if(eqUseTimeQ<eqUseTime){
                                            eqUseTimeQ=eqUseTime;
                                        }
                                        workNeedTime+= (((float) p.getCurrentProductionNum()/dayPlan*days)*p.getStandardWorkingHour());
                                    }else {
                                        long days = DateUtil.between(monthBegin, monthEnd, DateUnit.DAY);
                                        eqUseTime = (int) days * 12*60*60;
                                        if(eqUseTimeQ<eqUseTime){
                                            eqUseTimeQ=eqUseTime;
                                        }
                                        workNeedTime+= (((float) p.getCurrentProductionNum()/dayPlan*days)*p.getStandardWorkingHour());
                                    }

                                }
                            }
                        }

                    }
                    for (LoadVo p:prods){
                        if(p.getEqTypeCode()!=null) {
                            if (p.getEqTypeCode().equals(s)) {
                                Date planStartTime = p.getPlanStartTime();
                                Date planEndTime = p.getPlanEndTime();
                                eqTypeName = p.getEqTypeName();
                                boolean inStart = DateUtil.isIn(planStartTime, monthBegin, monthEnd);
                                boolean inEnd = DateUtil.isIn(planEndTime, monthBegin, monthEnd);
                                long dayPlan = DateUtil.between(planStartTime, planEndTime, DateUnit.DAY);
                                if (inStart) {
                                    if (inEnd) {

                                        eqUseTime = (int) dayPlan * 12 * 60 * 60;
                                        if (eqUseTimeQ < eqUseTime) {
                                            eqUseTimeQ = eqUseTime;
                                        }
                                        workNeedTime += (float) p.getUntranQuantity() * p.getStandardWorkingHour();
                                    } else {
                                        long days = DateUtil.between(planStartTime, monthEnd, DateUnit.DAY);
                                        eqUseTime = (int) days * 12 * 60 * 60;
                                        if (eqUseTimeQ < eqUseTime) {
                                            eqUseTimeQ = eqUseTime;
                                        }
                                        workNeedTime += (((float) p.getUntranQuantity() / dayPlan * days) * p.getStandardWorkingHour());
                                    }

                                } else {
                                    if (inEnd) {
                                        long days = DateUtil.between(monthBegin, planEndTime, DateUnit.DAY);
                                        eqUseTime = (int) days * 12 * 60 * 60;
                                        if (eqUseTimeQ < eqUseTime) {
                                            eqUseTimeQ = eqUseTime;
                                        }
                                        workNeedTime += (((float) p.getUntranQuantity() / dayPlan * days) * p.getStandardWorkingHour());
                                    } else {
                                        long days = DateUtil.between(monthBegin, monthEnd, DateUnit.DAY);
                                        eqUseTime = (int) days * 12 * 60 * 60;
                                        if (eqUseTimeQ < eqUseTime) {
                                            eqUseTimeQ = eqUseTime;
                                        }
                                        workNeedTime += (((float) p.getUntranQuantity() / dayPlan * days) * p.getStandardWorkingHour());
                                    }

                                }
                            }
                        }
                    }
                    for (EqTypeNumVo eqTypeNumVo:eqTypeNumVos){
                        if(eqTypeNumVo.getEqTypeName().equals(eqTypeName)){
                            eqUseTimeQ=eqUseTimeQ*eqTypeNumVo.getNum();
                        }
                    }
                    QingZbEqLoadTableEntity qingZbEqLoadTableEntity = new QingZbEqLoadTableEntity();

                    qingZbEqLoadTableEntity.setEqTypeName(eqTypeName);
                    qingZbEqLoadTableEntity.setWorkNeedTime(workNeedTime);
                    qingZbEqLoadTableEntity.setEqUseTime(eqUseTimeQ);
                    if(eqUseTimeQ==0){
                        qingZbEqLoadTableEntity.setLoadRatio(0.0F);
                    }else {
                        qingZbEqLoadTableEntity.setLoadRatio((workNeedTime/eqUseTimeQ));
                    }

                    DateTime parse = DateUtil.parse(now, "yyyy-MM");

                    QueryWrapper<QingZbEqLoadTableEntity> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("eq_type_code",s);
                    queryWrapper.eq("time",parse);

                    if(qingZbEqLoadTableMapper.selectOne(queryWrapper)!=null){
                        qingZbEqLoadTableMapper.update(qingZbEqLoadTableEntity,queryWrapper);
                    }else {
                        qingZbEqLoadTableEntity.setEqTypeCode(s);
                        qingZbEqLoadTableEntity.setTime(parse);
                        qingZbEqLoadTableMapper.insert(qingZbEqLoadTableEntity);
                    }
                }



            }

        //}
        QueryWrapper<QingZbEqLoadTableEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("time",workDate+"-01").orderByAsc("eq_type_code");
        List<QingZbEqLoadTableEntity> qingZbEqLoadTableEntities = qingZbEqLoadTableMapper.selectList(queryWrapper);
        return qingZbEqLoadTableEntities;


    }
}
